package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.warning.domain.dto.PortDTO;
import com.ruoyi.warning.domain.entity.Port;
import com.ruoyi.warning.domain.query.PortQuery;
import com.ruoyi.warning.service.IPortsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/ports")
@Api(tags = "端口", description = "用于处理端口相关操作的控制器")
public class PortsController {
    @Resource
    private IPortsService portsService;
    @ApiOperation("获取所有端口")
    @GetMapping(value = "/getAllPorts")
    public R getAllPorts(PortQuery query)
    {
        return R.ok(portsService.getAllPorts(query));
    }

    @ApiOperation("获取端口的详细信息")
    @GetMapping(value = "/getPortDetail/{id}")
    public R getPortDetail(@PathVariable String id)
    {
        return R.ok(portsService.getPortDetail(id));
    }

    @ApiOperation("获取端口的带宽利用率")
    @GetMapping(value = "/getPortBU/{id}")
    public R getPortBandwidthUtilization(@PathVariable String id)
    {
        return R.ok(portsService.getPortBandwidthUtilization(id));
    }

    @ApiOperation("获取端口的带宽利用率")
    @GetMapping(value = "/portStatusStatistics/{id}")
    public R portStatusStatistics(@PathVariable String id)
    {
        return R.ok(portsService.portStatusStatistics(id));
    }

    @ApiOperation("修改监控状态")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/updateIgnore/{id}")
    public R updateIgnore(@PathVariable String id)
    {
        Port port = portsService.getById(id);
        if(port.getIgnore()==0){
            port.setIgnore(1);
        }else{
            port.setIgnore(0);
        }
        portsService.updateById(port);
        return R.ok();
    }

    @ApiOperation("修改展示状态")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/updateIsShow/{id}")
    public R updateIsShow(@PathVariable String id)
    {
        Port port = portsService.getById(id);
        if(port.getIsShow()==0){
            port.setIsShow(1);
        }else{
            port.setIsShow(0);
        }
        portsService.updateById(port);
        return R.ok();
    }

    @ApiOperation("批量修改监控和展示状态")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping(value = "/updateIgnoreBatch")
    public R updateIgnoreBatch(@RequestBody PortDTO portDTO)
    {
        List<Port> ports = portsService.listByIds(portDTO.getIds());
        if(ObjectUtils.isNotEmpty(portDTO.getIgnore())){
            ports.forEach(port -> {
                if(portDTO.getIgnore()==0){
                    port.setIgnore(0);
                }else{
                    port.setIgnore(1);
                }
            });
        }
        if(ObjectUtils.isNotEmpty(portDTO.getIsShow())){
            ports.forEach(port -> {
                if(portDTO.getIsShow()==0){
                    port.setIsShow(0);
                }else {
                    port.setIsShow(1);
                }
            });
        }
        portsService.updateBatchById(ports);
        return R.ok();
    }

    @ApiOperation("修改关键链路状态")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/updateCritical/{id}")
    public R updateCritical(@PathVariable String id)
    {
        Port port = portsService.getById(id);
        if(port.getIsCritical()==0){
            port.setIsCritical(1);
        }else{
            port.setIsCritical(0);
        }
        portsService.updateById(port);
        return R.ok();
    }

    @ApiOperation("修改端口信息")
//    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping(value = "/updatePort")
    public R updatePort(@RequestBody Port port)
    {
        portsService.updateById(port);
        return R.ok();
    }
}
